package com.zzy.controller;

import com.zzy.domain.Category;
import com.zzy.domain.Result;
import com.zzy.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping
    public List<Category> list(){
        return categoryService.findAll();
    }
    
    /**
     * 新增分类
     */
    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        categoryService.add(category);
        return Result.success();
    }
    
    /**
     * 根据ID查询分类信息
     */
    @GetMapping("/show/{id}")
    public Result getById(@PathVariable Integer id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }
    
    /**
     * 更新分类信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }
    
    @DeleteMapping("/category/batch")
    public void deleteBatch(@RequestBody List<Integer> ids) {
        categoryService.deleteBatch(ids);
    }
    
    /**
     * 根据ID删除单个分类
     */
    @DeleteMapping("/category/{id}")
    public Result delete(@PathVariable Integer id) {
        categoryService.delete(id);
        return Result.success();
    }
}